﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
//
namespace PerceptiveMCAPI
{
	public class apiHelper
	{
		// ----------------------------------------------------------------------------------
		public static string FormatApiDateParm( DateTime date )
		{
			string strDate = ( date == DateTime.MinValue ) ? "" : date.ToString( "yyyy-MM-dd HH:mm:ss" );
			return strDate;
		}
		// ----------------------------------------------------------------------------------
		public static string MakeStringFromList( List<string> list )
		{
			if ( list.Count == 0 ) return string.Empty;
			//
			StringBuilder sb = new StringBuilder();
			foreach ( string entry in list )
			{
				sb.AppendFormat( "{0}, ", entry );
			}
			return sb.ToString( 0, sb.Length - 2 );
		}
		// ----------------------------------------------------------------------------------
		internal static string MakeStringFromObjectList( object objlist )
		{
			IEnumerable list = objlist as IEnumerable;
			if ( list == null ) return string.Empty;
			//
			StringBuilder sb = new StringBuilder();
			foreach ( var entry in list )
			{
				sb.AppendFormat( "{0}, ", entry.ToString() );
			}
			return sb.ToString( 0, sb.Length - 2 );
		}
		// ----------------------------------------------------------------------------------
		public static List<string> MakeListFromString( string string_value )
		{
			List<string> new_list = new List<string>();
			string[] delim = { ",", ";", "|", " " };
			string[] string_array = string_value.Split( delim, StringSplitOptions.RemoveEmptyEntries );
			foreach ( string entry in string_array )
			{
				new_list.Add( entry );
			}
			return new_list;
		}
		// ----------------------------------------------------------------------------------
		public static string DictionaryToString( Dictionary<string, string> dict )
		{
			if ( dict.Count == 0 ) return string.Empty;

			StringBuilder sb = new StringBuilder();
			foreach ( KeyValuePair<string, string> kv in dict )
			{
				string key = kv.Key;
				string val = kv.Value;

				sb.AppendFormat( "{0}=>{1}, ", key, val );
			}
			string result = sb.ToString( 0, sb.Length - 2 );
			return result;
		}
		// ----------------------------------------------------------------------------------
		public static List<T> MakeListFromArray<T>( T[] input )
		{
			List<T> oList = new List<T>();
			//
			foreach ( T entry in input )
			{
				oList.Add( entry );
			}
			return oList;
		}
		// ----------------------------------------------------------------------------------
		public static T[] MakeArrayFromList<T>( List<T> input )
		{
			T[] oArray = new T[input.Count];
			input.CopyTo( oArray );
			return oArray;
		}
		// ----------------------------------------------------------------------------------
		public static T GetEnumFromString<T>( string value )
		{
			T result = (T)Enum.Parse( typeof( T ), value );
			return result;
		}
		// ----------------------------------------------------------------------------------
		public static Dictionary<string, int> GetEnumValues<T>()
		{
			Dictionary<string, int> values = new Dictionary<string, int>();

			string[] names = Enum.GetNames( typeof( T ) );
			foreach ( string name in names )
			{
				int val = Convert.ToInt32( apiHelper.GetEnumFromString<T>( name ) );
				values.Add( name, val );
			}
			return values;
		}
		// ----------------------------------------------------------------------------------
		public static bool IsNumeric( object Expression )
		{
			bool isNum; double retNum;

			isNum = Double.TryParse( Convert.ToString( Expression ), System.Globalization.NumberStyles.Any,
				 System.Globalization.NumberFormatInfo.InvariantInfo, out retNum );

			return isNum;
		}
	}
}
